import 'package:flutter/material.dart';

enum IconTextButtonIconPosion {
  start, //icon在前
  end, //icon在后
}

// 自定义可调整图标位置的按钮
class IconTextButton extends StatelessWidget {
  const IconTextButton(
      {super.key,
      required this.icon,
      required this.label,
      this.axis = Axis.horizontal,
      this.mainAxisAlignment = MainAxisAlignment.center,
      this.crossAxisAlignment = CrossAxisAlignment.center,
      this.space = 2,
      this.iconPoison = IconTextButtonIconPosion.start,
      required this.onPressed});

  final Widget icon;
  final Widget label;
  // 布局方向 horizontal 从左到右  vertical 从上到下
  final Axis axis;
  // 主轴对齐方式
  final MainAxisAlignment mainAxisAlignment;
  // 交叉轴对齐方式
  final CrossAxisAlignment crossAxisAlignment;
  // 按钮与文字之间的间距
  final double space;
  final VoidCallback? onPressed;
  // icon位置
  final IconTextButtonIconPosion iconPoison;
  @override
  Widget build(BuildContext context) {
    SizedBox sizeBox = SizedBox(
      width: space,
      height: space,
    );
    List<Widget> childrens = [icon, sizeBox, label];
    if (iconPoison == IconTextButtonIconPosion.end) {
      //倒序
      childrens = childrens.reversed.toList();
    }

    if (axis == Axis.horizontal) {
      return InkWell(
          onTap: onPressed,
          child: Row(
            mainAxisAlignment: mainAxisAlignment,
            crossAxisAlignment: crossAxisAlignment,
            children: childrens,
          ));
    }

    return InkWell(
        onTap: onPressed,
        child: Column(
          mainAxisAlignment: mainAxisAlignment,
          crossAxisAlignment: crossAxisAlignment,
          children: childrens,
        ));
  }
}
